package com.zhq;


import android.Manifest;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;

import com.zhq.apputil.utils.PermissionManage;
import com.zhq.apputil.widget.NavigationMenuBar;
import com.zhq.apputil.widget.TitleBarLayout;
import com.zhq.base.BaseActivity;
import com.zhq.constant.FileConstant;
import com.zhq.ui.main.fragment.TabBarIndicatorFargment;
import com.zhq.ui.main.impl.IMainView;
import com.zhq.ui.main.presenter.MainMvpPresenter;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import butterknife.BindView;

public class MainActivity extends BaseActivity<IMainView, MainMvpPresenter> implements IMainView,NavigationMenuBar.OnTabSelectedListener {
    @BindView(R.id.navigation_menu_bar)
    NavigationMenuBar navigationMenuBar;
    private Context mContext;
    private List<Fragment> fragmentList = new ArrayList<>();

    @Override
    public int getLayoutUrl() {
        initImmersionBar(R.color.transparent);
        return R.layout.activity_main;
    }

    @Override
    public void registerById() {

    }

    @Override
    public TitleBarLayout getTitleBarLayout() {
        return null;
    }

    @Override
    public void initData() {
        createFileFolder();
        // android 7.0系统以上解决拍照的问题
        StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
        StrictMode.setVmPolicy(builder.build());
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            builder.detectFileUriExposure();
        }
        new PermissionManage.Builder(this)
                .setPermission(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.CAMERA
                })
                .builder()
                .requestPermission();
        navigationMenuBar.setBottomNameResource(new CharSequence[]{"指示器", "告警", "工单", "我的"})
                .setImageResource(
                        new int[]{R.drawable.home_tab_normal, R.drawable.home_tab_normal, R.drawable.home_tab_normal,
                                R.drawable.home_tab_normal},
                        new int[]{R.drawable.home_tab_pressed, R.drawable.home_tab_pressed, R.drawable.home_tab_pressed,
                                R.drawable.home_tab_pressed}
                )
                .setFontPadding(NavigationMenuBar.TOP, 5)
                .setImageWidth(25)
                .setBottomMenuBackgroundColor(getResources().getColor(R.color.white))
                .initToPosition(0)
                .builder();
        navigationMenuBar.setOnTabSelectedListener(this);
        TabBarIndicatorFargment tabBarIndicatorFargment = new TabBarIndicatorFargment();
        fragmentList.add(tabBarIndicatorFargment);
        fragmentList.add( new TabBarIndicatorFargment());
        fragmentList.add( new TabBarIndicatorFargment());
        fragmentList.add( new TabBarIndicatorFargment());
        getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, tabBarIndicatorFargment)
                .show(tabBarIndicatorFargment)
                .commit();
    }

    @Override
    protected MainMvpPresenter initPresenter() {
        return new MainMvpPresenter(this);
    }

    @Override
    public void upLoadSuccess(String sdkImageUrl) {

    }

    @Override
    public void onTabSelected(int currentPosition, int lastIndex) {
        if (fragmentList != null && currentPosition < fragmentList.size() && currentPosition != lastIndex) {
            FragmentManager fm = getSupportFragmentManager();
            FragmentTransaction ft = fm.beginTransaction();
            Fragment fragment = fragmentList.get(currentPosition);
            ft.hide(fragmentList.get(lastIndex));
            if (!fragment.isAdded()) {
                ft.add(R.id.fragment_container, fragment);
            }
            ft.show(fragment).commitAllowingStateLoss();
        }
    }

    /**
     * 创建App应用缓存文件夹
     **/
    public void createFileFolder() {
        File jsDir = new File(FileConstant.APP_DATA_FILE);
        if (!jsDir.exists()) {
            jsDir.mkdirs();
        }
    }

}
